//
//  ImageFileViewController.m
//  SHHP
//
//  Copyright (c) 2013年 epoint. All rights reserved.
//  显示图片预览：支持下载，保存操作

#import "WMH_NewsPic_ViewController.h"
#import "EpointAppDelegate.h"
#import "WMH_NewsDetail_ViewController.h"
@interface WMH_NewsPic_ViewController ()

@end

@implementation WMH_NewsPic_ViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        if([[UIScreen mainScreen]bounds].size.height==568){
            self = [super initWithNibName:@"WMH_NewsPic_ViewController5" bundle:nibBundleOrNil];
        }else{
            self = [super initWithNibName:@"WMH_NewsPic_ViewController" bundle:nibBundleOrNil];
        }
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.src = [initParam objectForKey:@"src"];
    self.infoTitle = [initParam objectForKey:@"infoTitle"];
    
    UIImage *image = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString: self.src]]];
//    [self.imageview setContentScaleFactor:[[UIScreen mainScreen] scale]];
//    self.imageview.contentMode =  UIViewContentModeScaleAspectFill;
//    self.imageview.autoresizingMask = UIViewAutoresizingFlexibleHeight;
//    self.imageview.clipsToBounds  = YES;
    self.imageview.image = image;
    self.view.backgroundColor = [UIColor blackColor];
}



- (IBAction)shareBtnPressed:(id)sender {

}

#pragma mark - 点击事件
- (IBAction)actionfx:(id)sender {
    NSLog(@"imagePath=%@",self.src);
    if ([self.src hasSuffix:@".jpg"] || [self.src hasSuffix:@".png"]) {
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
        NSString *filePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:[NSString stringWithFormat:@"tempSharePic.jpg"]];   // 保存文件的名称
        
        NSString * documentsDirectoryPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
        NSLog(@"保存路径:%@",documentsDirectoryPath);
        //Get Image From URL
        UIImage *image = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString: self.src]]];
        NSData *data;
        if (UIImagePNGRepresentation(image) == nil) {
            data = UIImageJPEGRepresentation(image, 1);
            filePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:[NSString stringWithFormat:@"tempSharePic.jpg"]];   // 保存文件的名称
            
            [data writeToFile:filePath atomically:YES];
        } else {
            data = UIImagePNGRepresentation(image);
            filePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:[NSString stringWithFormat:@"tempSharePic.png"]];   // 保存文件的名称
            
            [data writeToFile:filePath atomically:YES];
        }
        [self.parentView toSharedAction:self.infoTitle withURL:nil withImage:filePath];
    }
}

- (IBAction)actionbc:(id)sender {
    
    NSString * documentsDirectoryPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSLog(@"保存路径:%@",documentsDirectoryPath);
    //Get Image From URL
    UIImage *image = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString: self.src]]];
    
    NSLog(@"single click %@",image);
    UIImageWriteToSavedPhotosAlbum(image, nil, nil,nil);
    [self toastMessage:@"已保存到相册!"];
}

- (IBAction)actionback:(id)sender {
//    [self.dele back];
    [self dismissViewControllerAnimated:YES completion:nil];
}
@end
